<?php

namespace app\modules\v1\controllers;

use Yii;
use yii\rest\ActiveController;
use yii\web\NotFoundHttpException;
use app\modules\v1\models\Log;
use app\modules\v1\models\Userfile;
use app\modules\v1\models\Userdetail;
use app\modules\v1\models\Usermoney;
use app\modules\v1\models\Lawyer;

/**
 * json通用遵守
 * {
 *      "id":"",
 *      "telephone":"",
 *      "password":"",
 *      "status":"",
 *      "justtalk":"",
 *      "createdate":"",
 *      "token":""
 * }
 * (POST)注册
   {
       "id":"",
       "telephone":"13929718075",
       "password":"139297180751392971807513929718075",
       "status":"",
       "justtalk":"13929718075",
       "createdate":"",
       "token":"MjAxNy0wMy0wNQ==147e47817cd4daa2cd8668cd3b2a97f9"
  }
 * (PUT)密码修改
    {
       "password": "239297180751392971807513929718075",
       "token":"MjAxNy0wMy0wNw==8a72f5a9e00f51bc11a53c5aa871c7c6"
   }
 */
class UserController extends ActiveController
{
    public $modelClass = 'app\modules\v1\models\User';
    
    public function actions(){
        
        $actions = parent::actions();
        
        unset($actions['index'],$actions['create'],$actions['delete'],$actions['view'],$actions['update']);
        
    }
    
    public function actionIndex($telephone){
        
        $this->log('GET',json_encode(Yii::$app->request->get(),JSON_UNESCAPED_UNICODE));
        
        $modelClass = $this->modelClass;
        
        $model = $modelClass::find()->where(['telephone' => $telephone])->one();
        
        if (is_null($model)){
            $response = Yii::$app->response;
            $response->format = \yii\web\Response::FORMAT_JSON;
            return $response->data = ["telephone"=>404];
        }else{
            return $model;
        }
        
    }
    
    public function actionCreate(){
        
        $this->log('POST', Yii::$app->request->getRawBody());
        
        $model = new $this->modelClass;
        
        $data = json_decode(Yii::$app->request->getRawBody(),true);
        
        $id = time();
        
        $model->attributes = $data;
        
        $model->id = $id;
        
        $model->createdate = date('Y-m-d H:i:s');
        
        $this->tokenValidate($data['token']);
        
        if(Lawyer::find()->where(['telephone' => $data['telephone']])->one()){
            $response = Yii::$app->response;
            $response->format = \yii\web\Response::FORMAT_JSON;
            return $response->data = ['telephone' => 402];
        }else if(!$model->save()){
            return $model->getFirstErrors();
        }
        
        $UserFile = new Userfile;
        $UserInfo = new Userdetail;
        $UserMoney = new Usermoney;
        
        $UserFile->id = $id;
        $UserInfo->id = $id;
        $UserMoney->id = $id;
        
        $UserFile->attributes = ['telephone' => $data['telephone']];
        $UserInfo->attributes = ['telephone' => $data['telephone'], 'username' => "", 'age' => 0, 'sexy' => "", 'address' => "", 'email' => ""];
        $UserMoney->attributes = ['telephone' => $data['telephone'], 'money' => 0];
        
        if(!$UserFile->save()){
            return $UserFile->getFirstErrors();
        }
        if(!$UserInfo->save()){
            return $UserInfo->getFirstErrors();
        }
        if(!$UserMoney->save()){
            return $UserMoney->getFirstErrors();
        }
        
        return $model;
        
    }
    
    public function actionUpdate($id){
        
        $this->log('PUT', Yii::$app->request->getRawBody());

        $model = $this->findModel($id);
        
        $data = json_decode(Yii::$app->request->getRawBody(),true);
        
        $model->password = $data['password'];
        
        $this->tokenValidate($data['token']);
        
        if(!$model->save()){
            return $model->getFirstErrors();
        }
        
        return $model;
        
    }
    
    public function actionDelete($id){
        /*
        $this->log('DELETE', json_encode(['ID' => $id]));
        
        $model = $this->findModel($id);
        
        $model->delete();
        */
    }
    
    public function actionView($id){
        
        $this->log('GET', json_encode(['ID' => $id]));
        
        $model = $this->findModel($id);

        return $model;
        
    }
    
    protected function findModel($id){
        
        $modelClass = $this->modelClass;
        
        $model = $modelClass::findOne($id);
        
        if($model != null){
            return $model;
        }else{
            throw new NotFoundHttpException('404');
        }
        
    }
    
    protected function log($op,$data){
        
        $log = new Log();
        
        $log->attributes = ['operation' => $op,'class' => 'User','requestData' => $data,'dateTime' => date('Y-m-d H:i:s'),'ip' => Yii::$app->request->userIP];
        
        if(!$log->save()){
            return $log->getFirstError();
        }
        
    }
    
    public function tokenValidate($token){
        if($token != Yii::$app->params['token']){
            throw new NotFoundHttpException;
        }else{
            return true;
        }
    }

}
